﻿#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using BF;
using System.Diagnostics;
using System.Text;

public static class GitTools
{
    [MenuItem("Tools/Git/open git repository in explorer")]
    public static void OpenRepository()
    {
        string git_pro_path = new DirectoryInfo(Application.dataPath).Parent.Parent.FullName;

        Process.Start(git_pro_path).Dispose();
    }

    [MenuItem("Tools/Git/git pull")]
    public static void Pull()
    {
        Execute("git pull");
    }

    [MenuItem("Tools/Git/git push")]
    public static void Push()
    {
        Execute("git push");
    }

    [MenuItem("Tools/Git/git checkout .")]
    public static void Checkout()
    {
        Execute("git checkout .");
    }

    [MenuItem("Tools/Git/git clean -df")]
    public static void Clean()
    {
        Execute("git clean -df");
    }

    [MenuItem("Tools/Git/git reset --hard")]
    public static void ResetHard()
    {
        Execute("git checkout . && git clean -df && git reset --hard");
    }

    [MenuItem("Tools/Git/git gc")]
    public static void GC()
    {
        Execute("git gc --prune=now");
    }

    private static void Execute(string cmd)
    {
        UnityEngine.Debug.Log($"{cmd}");

        string git_pro_path = new DirectoryInfo(Application.dataPath).Parent.Parent.FullName;
        cmd = $"cd {git_pro_path} && {cmd}";

        CommandUtil.Result result = CommandUtil.Execute(cmd);
        UnityEngine.Debug.Log($"{result}");

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }


    [MenuItem("Tools/Git/convert all .cs lineend lf")]
    private static void ConvertAllCSLineEnd2LF()
    {
        DirectoryInfo dir = new DirectoryInfo(Application.dataPath);
        FileInfo[] files = dir.GetFiles("*.cs", SearchOption.AllDirectories);
        UnityEngine.Debug.Log("[Convert LF] Start");
        foreach (FileInfo file in files)
        {
            string fullName = file.FullName;

            Encoding encoding = EncodingUtils.GetEncoding(fullName);
            UnityEngine.Debug.Log("[Convert LF] " + fullName + ", " + encoding.EncodingName);



            string cs = File.ReadAllText(fullName, encoding);
            cs = cs.Replace("\r\n", "\n").Replace("\r", "\n");
            File.WriteAllText(fullName, cs, encoding);
        }

        UnityEngine.Debug.Log("[Convert LF] All Finish");
    }

    [MenuItem("Tools/Git/convert all .cs utf8")]
    private static void ConvertAllCS2UTF8()
    {
        DirectoryInfo dir = new DirectoryInfo(Application.dataPath);
        FileInfo[] files = dir.GetFiles("*.cs", SearchOption.AllDirectories);
        UnityEngine.Debug.Log("[Convert UTF8] Start");
        foreach (FileInfo file in files)
        {
            string fullName = file.FullName;
            UnityEngine.Debug.Log("[Convert UTF8] " + fullName);
            EncodingUtils.ConvertFileToUTF8(fullName);
        }

        UnityEngine.Debug.Log("[Convert UTF8] All Finish");
    }
}
#endif